﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace TinyForum.Infrastructure.Validation
{
    public class CustomRequiredAttribute : RequiredAttribute, IClientValidatable
    {
        #region IClientValidatable Members

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, 
            ControllerContext context)
        {
            string errorMessage = null;
            if (string.IsNullOrEmpty(this.ErrorMessage))
                errorMessage = string.Format("{0} is required.", metadata.DisplayName);
            else
                errorMessage = this.ErrorMessage;

            yield return new ModelClientValidationRule
            {
                ErrorMessage = errorMessage,
                ValidationType = "customrequired"
            };
        }

        #endregion
    }
}
